<?php

namespace Admin\Controller;

use Think\Controller;
use Common\Service\ApiService;  //接口服务调用api
class NewsController extends CommonController {
	/**
	 * 新闻列表 
	 */
    public function index() {
    	
    	$Model = M("News");
    	$total = $Model->where($map)->count();
    	$page = new \Think\Page($total, 10, $REQUEST);
    	// if($total>$listRows){
    	$page->setConfig('theme', ' %UP_PAGE% %LINK_PAGE% %DOWN_PAGE%  %HEADER%');
    	// }
    	$p = $page->show();
    	$this->assign('_page', $p ? $p : '');
    	$this->assign('_total', $total);
    	$order = "create_time desc";
    	$list = $Model->where($map)->order($order)->limit($page->firstRow . ',' . $page->listRows)->select();
    	$this->assign("_list", $list);
    	
    	
        $this->meta_title="课程列表";
        $this->display();
    }
    /**
     * 新闻添加
     */
    public function add(){
    	$model=D("News");
    	if (IS_POST){
    		if($model->create()){
    			if($model->add()){
    				$this->success("操作成功",U("index"));
    			}else{
    				$this->error("操作失败");
    			}
    		}else{
    			$this->error("操作失败");
    		}
    	}
    	$this->meta_title="课程添加";
    	$this->display();
    }
    
    /**
     * 改变数据库操作
     */
    public function changestatus() {
    	$method = I("method");
    	$id = I("id", 0);
    	$status = I("status");
    	if ($status == 1) {
    		$status = 0;
    	} else {
    		$status = 1;
    	}
    	switch ($method) {
    		case "cgStatus": //审核状态
    			$data["status"] = $status;
    			
    		   if(M("News")->where(array("id"=>$id))->save($data)){
    		   	$this->success("操作成功");
    		   }else{
    		   	$this->error("操作失败");
    		   }
    			
    			
    			break;
    		case "cgIstop" :  //是否置顶
    			$data["istop"] = $status;
    
    			if(M("News")->where(array("id"=>$id))->save($data)){
    				$this->success("操作成功");
    			}else{
    				$this->error("操作失败");
    			}
    			break;
    		case "cgRecommended" :  //是否推荐
    			$data["recommended"] = $status;
    			if(M("News")->where(array("id"=>$id))->save($data)){
    				$this->success("操作成功");
    			}else{
    				$this->error("操作失败");
    			}
    			break;
    
    		case "deleteArticle" :  //删除文章
    			if(M("News")->where(array("id"=>$id))->save($data)){
    				$this->success("操作成功");
    			}else{
    				$this->error("操作失败");
    			}
    			break;
    		case "recoveryArticle":   //恢复文章
    			$data["status"] = $status;
    			if(M("News")->where(array("id"=>$id))->save($data)){
    				$this->success("操作成功");
    			}else{
    				$this->error("操作失败");
    			}
    			break;
    	}
    }
    /**
     * 新闻编辑
     */
    public function edit(){
    	if (IS_POST){
    		$model=M("News");
    		if($model->create()){
    			if($model->save()){
    				$this->success("操作成功",U("index"));
    			}else{
    				$this->error("操作失败");
    			}
    		}else{
    			$this->error("操作失败");
    		}
    	}
    	$map["id"]=I("id");
    	$Model=M("News");
    	$info=$Model->where($map)->find();
    	$this->assign("info",$info);
    	$this->meta_title="课程编辑";
    	$this->display();
    }
    /**
     * 新闻删除
     */
    public function del($id){
    	$Model=M("News");
    	if($Model->where(array("id"=>$id))->delete()){
    		$this->success("操作成功");
    	}else{
    		$this->error("操作失败");
    	}
    }
   
   
}
